package role

import (
	"github.com/astaxie/beego/orm"
	"github.com/astaxie/beego/validation"
	"qycms/controllers"
	"qycms/models/auth"
	"qycms/untils"
	"strings"
)

type RoleController struct {
	controllers.BaseController
}
/**
 *  角色列表主页
 */
func (this *RoleController) Index()  {
	this.TplName = "role/index.html"
}

/**
 *  角色列表数据
 */
func (this *RoleController) RoleList()  {
	//分页计算
	currentPage,err := this.GetInt("page",1)
	pagePerNum,_ := this.GetInt("limit",20)
	if(err != nil ){
		currentPage = 1
	}
	offsetNum := pagePerNum * (currentPage - 1)

	//数据查询
	var count int64 = 0
	roles := []auth.Role{}

	O := orm.NewOrm()
	q := O.QueryTable("sys_role")

	keyword := this.GetString("key")
	if keyword != ""{
		q = q.Filter("role_name__icontains",keyword)
	}

	count,_ = q.Filter("is_delete",0).Count()
	q.Filter("is_delete",0).Limit(pagePerNum).Offset(offsetNum).All(&roles)

	this.Data["json"] = untils.ListJson(count,roles)
	this.ServeJSON()
	this.StopRun()
}

/**
 *  新建角色页面
 */
func (this *RoleController) AddRoleView()  {
	this.TplName = "role/add.html"
}

/**
 *  新建角色
 */
func (this *RoleController) AddRole()  {
	//接收参数
	role_name := this.GetString("role_name")
	is_active,_ := this.GetInt("is_active")
	desc := this.GetString("desc")

	//数据验证
	valid := validation.Validation{}

	valid.Required(role_name,"role_name").Message("角色名称不能为空")
	valid.Required(is_active,"is_active").Message("角色状态不能为空")
	valid.Required(desc,"desc").Message("备注不能为空")

	o := orm.NewOrm()
	//角色名称唯一性验证
	isrole := o.QueryTable("sys_role").Filter("role_name",role_name).Filter("is_delete",0).Exist()

	if isrole {
		this.Err(2001,"角色名称已经存在",nil)
	}

	//绑定到模型
	role := auth.Role{
		RoleName: role_name,
		Desc: desc,
		IsActive: is_active,
	}

	_ ,err := o.Insert(&role)

	if err != nil {
		this.Err(2001,"操作失败",nil)
	}else{
		this.Suc(nil)
	}
}

/**
 *  更新角色页面
 */
func (this *RoleController) UpdateRoleView()  {
	id,_ := this.GetInt("id")

	o := orm.NewOrm()
    role := auth.Role{}
    o.QueryTable("sys_role").Filter("id",id).Filter("is_delete",0).One(&role)
    this.Data["role"] = role
	this.TplName = "role/edit.html"
}

/**
 *  更新角色
 */
func (this *RoleController) UpdateRole()  {
	//接收参数
	ID := this.GetString("Id")
	role_name := this.GetString("role_name")
	is_active := this.GetString("is_active")
	desc := this.GetString("desc")

	//数据验证
	valid := validation.Validation{}
	valid.Required(ID,"Id").Message("请选择修改的数据")
	valid.Required(role_name,"role_name").Message("角色名称不能为空")
	valid.Required(is_active,"is_active").Message("角色状态不能为空")
	valid.Required(desc,"desc").Message("备注不能为空")

	o := orm.NewOrm()
	//角色名称唯一性验证
	isrole := o.QueryTable("sys_role").Filter("role_name",role_name).Filter("is_delete",0).Exclude("id",ID).Exist()

	if isrole {
		this.Err(2001,"角色名称已经存在",nil)
	}

	//绑定到模型
	//绑定到模型
	var new_is_active int
	if is_active == "on"{
		new_is_active = 1
	}else{
		new_is_active = 0
	}

	role := orm.Params{
		"role_name": role_name,
		"desc": desc,
		"is_active": new_is_active,
	}

	_ ,err := o.QueryTable("sys_role").Filter("id",ID).Update(role)

	if err != nil {
		this.Err(2001,"操作失败",nil)
	}else{
		this.Suc(nil)
	}
}

/**
 *  删除角色
 */
func (this *RoleController) DelRole()  {
	//接收参数
	ids := this.GetString("ids");
	new_ids := ids[:len(ids)]
	id_arr := strings.Split(new_ids,",")

	//数据更新
	o := orm.NewOrm()
	qs := o.QueryTable("sys_role")
	for _,v :=range id_arr{
		id_int := untils.StrToInt(v)
		qs.Filter("id",id_int).Update(orm.Params{
			"is_delete":1,
		})
	}
	this.Suc(nil)
}